package com.yazi.mxz.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;

import mo.framework.smh.hibernate.PageInfo;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.hcd365.hcd.client.Config;
import com.hcd365.hcd.common.entity.beans.Ad;
import com.hcd365.hcd.common.entity.beans.Address;
import com.hcd365.hcd.common.entity.beans.Express;
import com.hcd365.hcd.common.entity.beans.Member;
import com.hcd365.hcd.common.entity.beans.Category;
import com.hcd365.hcd.common.entity.beans.MemberNotice;
import com.hcd365.hcd.common.entity.beans.Notice;
import com.hcd365.hcd.common.entity.beans.PayType;
import com.hcd365.hcd.common.entity.beans.Region;
import com.hcd365.hcd.common.entity.beans.SecurityQuestion;
import com.hcd365.hcd.common.entity.beans.Shopping;
import com.hcd365.hcd.common.entity.beans.ShoppingCart;
import com.hcd365.hcd.common.entity.beans.Store;
import com.hcd365.hcd.common.entity.beans.StoreGoods;
import com.hcd365.hcd.common.entity.beans.StoreGoodsPic;
import com.hcd365.hcd.common.entity.beans.StorePromote;
import com.hcd365.hcd.server.response.AdListResponse;
import com.hcd365.hcd.server.response.AddressListResponse;
import com.hcd365.hcd.server.response.AddressResponse;
import com.hcd365.hcd.server.response.CategoryListResponse;
import com.hcd365.hcd.server.response.IntegerResponse;
import com.hcd365.hcd.server.response.MemberNoticeListResponse;
import com.hcd365.hcd.server.response.MemberResponse;
import com.hcd365.hcd.server.response.OrderAddFormData;
import com.hcd365.hcd.server.response.OrderAddFormResponse;
import com.hcd365.hcd.server.response.OrderAddResponse;
import com.hcd365.hcd.server.response.RegionListResponse;
import com.hcd365.hcd.server.response.RegisterResponse;
import com.hcd365.hcd.server.response.SecurityQuestionListResponse;
import com.hcd365.hcd.server.response.ShoppingAddResponse;
import com.hcd365.hcd.server.response.ShoppingCartListResponse;
import com.hcd365.hcd.server.response.StatusResponse;
import com.hcd365.hcd.server.response.StoreGoodsListResponse;
import com.hcd365.hcd.server.response.StoreGoodsResponse;
import com.hcd365.hcd.server.response.StoreListResponse;

/**
 * 
 * @author mo_yq5
 * @date 2014-5-13
 */
public class Client extends DefaultHttpClient {
	private static JsonFactory jsonFactory = new JsonFactory();
	private Config config;
	private ObjectMapper om;

	Client(Config config) {
		this.config = config;
		om = new ObjectMapper();
		om.setSerializationInclusion(Include.NON_EMPTY);
		om.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
		//om.addMixInAnnotations(Set.class, ArrayList.class);
	}

	public <T extends StatusResponse> T post(String url, Object reqObject,
			Class<T> resClass) throws ClientProtocolException,
			JsonProcessingException, IOException {
		String reqString;
		if (reqObject instanceof String) {
			reqString = String.valueOf(reqObject);
		} else {
			System.out.println("java2json前:" + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()));
			reqString = om.writerWithDefaultPrettyPrinter().writeValueAsString(reqObject);
			System.out.println("java2json后:" + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()));
		}
		if (config.getIsPrintBody()) {
			System.out.println(new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()) + " 请求[" + config.getPath() + url + "] => " + reqString);
		}
		
		StringEntity reqEntity = new StringEntity(reqString,
				config.getEncoding());
		HttpPost httpPost = new HttpPost(config.getPath() + url);
		httpPost.addHeader("Content-Type", config.getConentType());
		httpPost.setEntity(reqEntity);
		HttpResponse res = execute(httpPost);
		String resString = getResponseBodyAsString(res);
		if (config.getIsPrintBody()) {
			System.out.println(new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()) + " 响应 => "  + resString);
		}
		
		String className = resClass.getSimpleName();
		System.out.println(className + " -> json2java前:" + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()));
		T t = null;
		if (resClass.getSimpleName().equals("MemberResponse")) {
			t = (T)loginRes(resString);
		} else if (resClass.getSimpleName().equals("CategoryListResponse")) {
			t = (T)categoryListRes(resString);
		} else if (resClass.getSimpleName().equals("StoreGoodsListResponse")) {
			t = (T)storeGoodsListRes(resString);
		} else {
			t = om.readValue(resString, resClass);
		}
		System.out.println(className + " -> json2java后:" + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()));
		return t;
	}
	
	public <T extends StatusResponse> T post2(String url, String reqBody,
			Class<T> resClass) throws ClientProtocolException,
			JsonProcessingException, IOException {
		
		if (config.getIsPrintBody()) {
			System.out.println(new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()) + " 请求[" + config.getPath() + url + "] => " + reqBody);
		}
		
		HttpPost httpPost = new HttpPost(config.getPath() + url);
		httpPost.addHeader("Content-Type", config.getConentType());
		if (null != reqBody) {
			StringEntity reqEntity = new StringEntity(reqBody,
					config.getEncoding());
			httpPost.setEntity(reqEntity);
			
		}
		HttpResponse res = execute(httpPost);
		String resString = getResponseBodyAsString(res);
		if (config.getIsPrintBody()) {
			System.out.println(new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()) + " 响应 => "  + resString);
		}
		String className = resClass.getSimpleName();
		System.out.println(className + " -> json2java前:" + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()));
		T t = null;
		if (className.equals("StatusResponse")) {
			t = (T)statusRes(resString);
		} else if (className.equals("StoreListResponse")) {
			t = (T)storeListRes(resString);
		} else if (className.equals("RegisterResponse")) {
			t = (T)registerRes(resString);
		} else if (className.equals("MemberResponse")) {
			t = (T)loginRes(resString);
		} else if (className.equals("CategoryListResponse")) {
			t = (T)categoryListRes (resString);
		} else if (className.equals("StoreGoodsListResponse")) {
			t = (T)storeGoodsListRes(resString);
		} else if (className.equals("AdListResponse")) {
			t = (T)adListRes(resString);
		} else if (className.equals("StoreGoodsResponse")) {
			t = (T)storeGoodsRes(resString);
		} else if (className.equals("RegionListResponse")) {
			t = (T)regionListRes(resString);
		} else if (className.equals("AddressResponse")) {
			t = (T)addressRes(resString);
		} else if (className.equals("AddressListResponse")) {
			t = (T)addressListRes(resString);
		} else if (className.equals("ShoppingAddResponse")) {
			t = (T)shoppingAddRes(resString);
		} else if (className.equals("IntegerResponse")) {
			t = (T)integerRes(resString);
		} else if (className.equals("ShoppingCartListResponse")) {
			t = (T)cartListRes(resString);
		} else if (className.equals("OrderAddFormResponse")) {
			t = (T)orderAddFormRes(resString);
		} else if (className.equals("OrderAddResponse")) {
			t = (T)orderAddRes(resString);
		} else if (className.equals("MemberNoticeListResponse")) {
			t = (T)memberNoticeListRes(resString);
		} else if (className.equals("SecurityQuestionListResponse")) {
			t = (T)securityQuestionListRes(resString);
		} else {
			t = om.readValue(resString, resClass);
		}
		System.out.println(className + " -> json2java后:" + new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()));
		return t;
	}
	
	private StatusResponse statusRes(String jsonString){
		StatusResponse res = new StatusResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private StoreListResponse storeListRes(String jsonString) {
		StoreListResponse res = new StoreListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getLongValue());
				} else if ("successData".equals(fieldname)) {
					List<Store> list = new ArrayList<Store>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						Store store = new Store();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("isNewOrder".equals(nameField)) {
								store.setIsNewOrder(jp.getBooleanValue());
							} else if ("name".equals(nameField)) {
								store.setName(jp.getText());
							} else if ("storeId".equals(nameField)) {
								store.setStoreId(jp.getLongValue());
							} 
						}
						list.add(store);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private RegisterResponse registerRes(String jsonString){
		RegisterResponse res = new RegisterResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getLongValue());
				} else if ("successData".equals(fieldname)) {
					Member member = new Member();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("memberId".equals(nameField)) {
							member.setMemberId(jp.getValueAsLong());
						} else if ("name".equals(nameField)) {
							member.setName(jp.getText());
						} else if ("integral".equals(nameField)) {
							member.setIntegral(jp.getValueAsInt());
						} else if ("email".equals(nameField)) {
							member.setEmail(jp.getText());
						} else if ("sex".equals(nameField)) {
							member.setSex(jp.getValueAsInt());
						} else if ("birthday".equals(nameField)) {
							member.setBirthday(jp.getValueAsLong());
						} else if ("income".equals(nameField)) {
							member.setIncome(jp.getFloatValue());
						} else if ("marriage".equals(nameField)) {
							member.setMarriage(jp.getValueAsInt());
						} else if ("regionId".equals(nameField)) {
							member.setRegionId(jp.getValueAsLong());
						} else if ("storeId".equals(nameField)) {
							member.setStoreId(jp.getValueAsLong());
						} else if ("loginTime".equals(nameField)) {
							member.setLoginTime(jp.getValueAsLong());
						} else if ("registerTime".equals(nameField)) {
							member.setRegisterTime(jp.getValueAsLong());
						} else if ("passwordChecker".equals(nameField)) {
							member.setPasswordChecker(jp.getValueAsInt());
						} else if ("emailStatus".equals(nameField)) {
							member.setEmailStatus(jp.getValueAsInt());
						} else if ("shakeCount".equals(nameField)) {
							member.setShakeCount(jp.getValueAsInt());
						} else if ("shakeTime".equals(nameField)) {
							member.setShakeTime(jp.getValueAsLong());
						} 
					}
					res.setSuccessData(member);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private MemberResponse loginRes(String jsonString){
		MemberResponse res = new MemberResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					Member member = new Member();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("memberId".equals(nameField)) {
							member.setMemberId(jp.getValueAsLong());
						} else if ("name".equals(nameField)) {
							member.setName(jp.getText());
						} else if ("integral".equals(nameField)) {
							member.setIntegral(jp.getValueAsInt());
						} else if ("email".equals(nameField)) {
							member.setEmail(jp.getText());
						} else if ("sex".equals(nameField)) {
							member.setSex(jp.getValueAsInt());
						} else if ("birthday".equals(nameField)) {
							member.setBirthday(jp.getValueAsLong());
						} else if ("income".equals(nameField)) {
							member.setIncome(jp.getFloatValue());
						} else if ("marriage".equals(nameField)) {
							member.setMarriage(jp.getValueAsInt());
						} else if ("regionId".equals(nameField)) {
							member.setRegionId(jp.getValueAsLong());
						} else if ("storeId".equals(nameField)) {
							member.setStoreId(jp.getValueAsLong());
						} else if ("loginTime".equals(nameField)) {
							member.setLoginTime(jp.getValueAsLong());
						} else if ("registerTime".equals(nameField)) {
							member.setRegisterTime(jp.getValueAsLong());
						} else if ("passwordChecker".equals(nameField)) {
							member.setPasswordChecker(jp.getValueAsInt());
						} else if ("emailStatus".equals(nameField)) {
							member.setEmailStatus(jp.getValueAsInt());
						} else if ("shakeCount".equals(nameField)) {
							member.setShakeCount(jp.getValueAsInt());
						} else if ("shakeTime".equals(nameField)) {
							member.setShakeTime(jp.getValueAsLong());
						} 
					}
					res.setSuccessData(member);
				}
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private CategoryListResponse categoryListRes(String jsonString) {
		CategoryListResponse res = new CategoryListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					List<Category> list = new ArrayList<Category>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						Category cat = new Category();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("catId".equals(nameField)) {
								cat.setCatId(jp.getValueAsLong());
							} else if ("type".equals(nameField)) {
								cat.setType(jp.getValueAsInt());
							} else if ("name".equals(nameField)) {
								cat.setName(jp.getText());
							} else if ("deep".equals(nameField)) {
								cat.setDeep(jp.getValueAsInt());
							} else if ("pic".equals(nameField)) {
								cat.setPic(jp.getText());
							} else if ("sort".equals(nameField)) {
								cat.setSort(jp.getValueAsLong());
							} else if ("categories".equals(nameField)) {
								Set<Category> categories = new HashSet<Category>();
								while (jp.nextToken() != JsonToken.END_ARRAY) {
									Category childCat = new Category();
									while (jp.nextToken() != JsonToken.END_OBJECT) {
										nameField = jp.getCurrentName();
										jp.nextToken();
										if ("catId".equals(nameField)) {
											childCat.setCatId(jp.getValueAsLong());
										} else if ("type".equals(nameField)) {
											childCat.setType(jp.getValueAsInt());
										} else if ("name".equals(nameField)) {
											childCat.setName(jp.getText());
										} else if ("deep".equals(nameField)) {
											childCat.setDeep(jp.getValueAsInt());
										} else if ("pic".equals(nameField)) {
											childCat.setPic(jp.getText());
										} else if ("sort".equals(nameField)) {
											childCat.setSort(jp.getValueAsLong());
										}
										categories.add(childCat);
									}	
								}
								cat.setCategories(categories);
							}
						}
						list.add(cat);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private StoreGoodsListResponse storeGoodsListRes(String jsonString) {
		StoreGoodsListResponse res = new StoreGoodsListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("pageInfo".equals(fieldname)) {
					PageInfo pi = new PageInfo();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("pageSize".equals(nameField)) {
							pi.setPageSize(jp.getValueAsInt());
						} else if ("nowPage".equals(nameField)) {
							pi.setNowPage(jp.getValueAsInt());
						} else if ("totalResult".equals(nameField)) {
							pi.setTotalResult(jp.getValueAsInt());
						} else if ("orderField".equals(nameField)) {
							pi.setOrderField(jp.getText());
						} else if ("linkCount".equals(nameField)) {
							pi.setLinkCount(jp.getValueAsInt());
						} else if ("orderType".equals(nameField)) {
							pi.setOrderType(jp.getValueAsInt());
						}
					}
					res.setPageInfo(pi);
				} else if ("successData".equals(fieldname)) {
					List<StoreGoods> list = new ArrayList<StoreGoods>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						StoreGoods sg = new StoreGoods();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("storeGoodsId".equals(nameField)) {
								sg.setStoreGoodsId(jp.getValueAsLong());
							} else if ("code".equals(nameField)) {
								sg.setCode(jp.getText());
							} else if ("name".equals(nameField)) {
								sg.setName(jp.getText());
							} else if ("sellStatus".equals(nameField)) {
								sg.setSellStatus(jp.getValueAsInt());
							} else if ("unit".equals(nameField)) {
								sg.setUnit(jp.getText());
							}else if ("price".equals(nameField)) {
								sg.setPrice(jp.getFloatValue());
							} else if ("stock".equals(nameField)) {
								sg.setStock(jp.getFloatValue());
							} else if ("buyDef".equals(nameField)) {
								sg.setBuyDef(jp.getFloatValue());
							} else if ("buyMin".equals(nameField)) {
								sg.setBuyMin(jp.getFloatValue());
							} else if ("buyExt".equals(nameField)) {
								sg.setBuyExt(jp.getFloatValue());
							} else if ("promotePrice".equals(nameField)) {
								sg.setPromotePrice(jp.getFloatValue());
							} else if ("promoteType".equals(nameField)) {
								sg.setPromoteType(jp.getValueAsInt());
							} else if ("pic".equals(nameField)) {
								sg.setPic(jp.getText());
							} else if ("focusOrder".equals(nameField)) {
								sg.setFocusOrder(jp.getValueAsInt());
							} else if ("focusStatus".equals(nameField)) {
								sg.setFocusStatus(jp.getValueAsInt());
							} else if ("sellTime".equals(nameField)) {
								sg.setSellTime(jp.getValueAsLong());
							} else if ("dispatch".equals(nameField)) {
								sg.setDispatch(jp.getText());
							} else if ("dealAll".equals(nameField)) {
								sg.setDealAll(jp.getValueAsInt());
							} else if ("dealMonth".equals(nameField)) {
								sg.setDealMonth(jp.getValueAsInt());
							} else if ("dealTime".equals(nameField)) {
								sg.setDealTime(jp.getValueAsLong());
							} else if ("description".equals(nameField)) {
								sg.setDescription(jp.getText());
							} else if ("updateTime".equals(nameField)) {
								sg.setUpdateTime(jp.getValueAsLong());
							} else if ("addTime".equals(nameField)) {
								sg.setAddTime(jp.getValueAsLong());
							} else if ("keywords".equals(nameField)) {
								sg.setKeywords(jp.getText());
							} else if ("buyUnit".equals(nameField)) {
								sg.setBuyUnit(jp.getText());
							} else if ("buyUnitMin".equals(nameField)) {
								sg.setBuyUnitMin(jp.getValueAsDouble());
							} else if ("buyUnitMax".equals(nameField)) {
								sg.setBuyUnitMax(jp.getValueAsDouble());
							} else if ("sort".equals(nameField)) {
								sg.setSort(jp.getValueAsLong());
							} else if ("dealWeek".equals(nameField)) {
								sg.setDealWeek(jp.getValueAsInt());
							}
						}
						list.add(sg);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private AdListResponse adListRes(String jsonString) {
		AdListResponse res = new AdListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("pageInfo".equals(fieldname)) {
					PageInfo pi = new PageInfo();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("pageSize".equals(nameField)) {
							pi.setPageSize(jp.getValueAsInt());
						} else if ("nowPage".equals(nameField)) {
							pi.setNowPage(jp.getValueAsInt());
						} else if ("totalResult".equals(nameField)) {
							pi.setTotalResult(jp.getValueAsInt());
						} else if ("orderField".equals(nameField)) {
							pi.setOrderField(jp.getText());
						} else if ("linkCount".equals(nameField)) {
							pi.setLinkCount(jp.getValueAsInt());
						} else if ("orderType".equals(nameField)) {
							pi.setOrderType(jp.getValueAsInt());
						}
					}
					res.setPageInfo(pi);
				} else if ("successData".equals(fieldname)) {
					List<Ad> list = new ArrayList<Ad>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						Ad ad = new Ad();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("addTime".equals(nameField)) {
								ad.setAddTime(jp.getLongValue());
							} else if ("adId".equals(nameField)) {
								ad.setAdId(jp.getLongValue());
							} else if ("devType".equals(nameField)) {
								ad.setDevType(jp.getIntValue());
							} else if ("expiredTime".equals(nameField)) {
								ad.setExpiredTime(jp.getLongValue());
							} else if ("pic".equals(nameField)) {
								ad.setPic(jp.getText());
							} else if ("status".equals(nameField)) {
								ad.setStatus(jp.getBooleanValue());
							} else if ("storeId".equals(nameField)) {
								ad.setStoreId(jp.getLongValue());
							} else if ("summary".equals(nameField)) {
								ad.setSummary(jp.getText());
							} else if ("title".equals(nameField)) {
								ad.setTitle(jp.getText());
							} else if ("type".equals(nameField)) {
								ad.setType(jp.getIntValue());
							} else if ("url".equals(nameField)) {
								ad.setUrl(jp.getText());
							}
						}
						list.add(ad);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private StoreGoodsResponse storeGoodsRes(String jsonString) {
		StoreGoodsResponse res = new StoreGoodsResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					StoreGoods sg = new StoreGoods();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("storeGoodsId".equals(nameField)) {
							sg.setStoreGoodsId(jp.getValueAsLong());
						} else if ("code".equals(nameField)) {
							sg.setCode(jp.getText());
						} else if ("name".equals(nameField)) {
							sg.setName(jp.getText());
						} else if ("sellStatus".equals(nameField)) {
							sg.setSellStatus(jp.getValueAsInt());
						} else if ("unit".equals(nameField)) {
							sg.setUnit(jp.getText());
						} else if ("price".equals(nameField)) {
							sg.setPrice(jp.getFloatValue());
						} else if ("stock".equals(nameField)) {
							sg.setStock(jp.getFloatValue());
						} else if ("buyDef".equals(nameField)) {
							sg.setBuyDef(jp.getFloatValue());
						} else if ("buyMin".equals(nameField)) {
							sg.setBuyMin(jp.getFloatValue());
						} else if ("buyExt".equals(nameField)) {
							sg.setBuyExt(jp.getFloatValue());
						} else if ("promotePrice".equals(nameField)) {
							sg.setPromotePrice(jp.getFloatValue());
						} else if ("promoteType".equals(nameField)) {
							sg.setPromoteType(jp.getValueAsInt());
						} else if ("pic".equals(nameField)) {
							sg.setPic(jp.getText());
						} else if ("focusOrder".equals(nameField)) {
							sg.setFocusOrder(jp.getValueAsInt());
						} else if ("focusStatus".equals(nameField)) {
							sg.setFocusStatus(jp.getValueAsInt());
						} else if ("sellTime".equals(nameField)) {
							sg.setSellTime(jp.getValueAsLong());
						} else if ("dispatch".equals(nameField)) {
							sg.setDispatch(jp.getText());
						} else if ("dealAll".equals(nameField)) {
							sg.setDealAll(jp.getValueAsInt());
						} else if ("dealMonth".equals(nameField)) {
							sg.setDealMonth(jp.getValueAsInt());
						} else if ("dealTime".equals(nameField)) {
							sg.setDealTime(jp.getValueAsLong());
						} else if ("description".equals(nameField)) {
							sg.setDescription(jp.getText());
						} else if ("updateTime".equals(nameField)) {
							sg.setUpdateTime(jp.getValueAsLong());
						} else if ("addTime".equals(nameField)) {
							sg.setAddTime(jp.getValueAsLong());
						} else if ("keywords".equals(nameField)) {
							sg.setKeywords(jp.getText());
						} else if ("buyUnit".equals(nameField)) {
							sg.setBuyUnit(jp.getText());
						} else if ("buyUnitMin".equals(nameField)) {
							sg.setBuyUnitMin(jp.getValueAsDouble());
						} else if ("buyUnitMax".equals(nameField)) {
							sg.setBuyUnitMax(jp.getValueAsDouble());
						} else if ("sort".equals(nameField)) {
							sg.setSort(jp.getValueAsLong());
						} else if ("dealWeek".equals(nameField)) {
							sg.setDealWeek(jp.getValueAsInt());
						} else if ("storePromote".equals(fieldname)) {
							StorePromote sp = new StorePromote();
							while (jp.nextToken() != JsonToken.END_OBJECT) {
								nameField = jp.getCurrentName();
								jp.nextToken();
								if ("endTime".equals(nameField)) {
									sp.setEndTime(jp.getLongValue());
								} else if ("price".equals(nameField)) {
									sp.setPrice(jp.getFloatValue());
								} else if ("promoteType".equals(nameField)) {
									sp.setPromoteType(jp.getIntValue());
								} else if ("startTime".equals(nameField)) {
									sp.setStartTime(jp.getLongValue());
								} else if ("status".equals(nameField)) {
									sp.setStatus(jp.getIntValue());
								} else if ("storeGoodsId".equals(nameField)) {
									sp.setStoreGoodsId(jp.getLongValue());
								} else if ("summary".equals(nameField)) {
									sp.setSummary(jp.getText());
								} else if ("unit".equals(nameField)) {
									sp.setUnit(jp.getText());
								} 
							}
							sg.setStorePromote(sp);
							
						} else if ("storeGoodsPics".equals(nameField)) {
							Set<StoreGoodsPic> pics = new HashSet<StoreGoodsPic>();
							while (jp.nextToken() != JsonToken.END_ARRAY) {
								StoreGoodsPic pic = new StoreGoodsPic();
								while (jp.nextToken() != JsonToken.END_OBJECT) {
									nameField = jp.getCurrentName();
									jp.nextToken();
									if ("pic".equals(nameField)) {
										pic.setPic(jp.getText());
									} else if ("picId".equals(nameField)) {
										pic.setPicId(jp.getLongValue());
									} 
									pics.add(pic);
								}	
							}
							sg.setStoreGoodsPics(pics);
						}
						
					}
					res.setSuccessData(sg);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private RegionListResponse regionListRes(String jsonString) {
		RegionListResponse res = new RegionListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("pageInfo".equals(fieldname)) {
					PageInfo pi = new PageInfo();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("pageSize".equals(nameField)) {
							pi.setPageSize(jp.getValueAsInt());
						} else if ("nowPage".equals(nameField)) {
							pi.setNowPage(jp.getValueAsInt());
						} else if ("totalResult".equals(nameField)) {
							pi.setTotalResult(jp.getValueAsInt());
						} else if ("orderField".equals(nameField)) {
							pi.setOrderField(jp.getText());
						} else if ("linkCount".equals(nameField)) {
							pi.setLinkCount(jp.getValueAsInt());
						} else if ("orderType".equals(nameField)) {
							pi.setOrderType(jp.getValueAsInt());
						}
					}
					res.setPageInfo(pi);
				} else if ("successData".equals(fieldname)) {
					List<Region> list = new ArrayList<Region>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						Region region = new Region();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("agencyId".equals(nameField)) {
								region.setAgencyId(jp.getLongValue());
							} else if ("parentId".equals(nameField)) {
								region.setParentId(jp.getLongValue());
							} else if ("regionId".equals(nameField)) {
								region.setRegionId(jp.getLongValue());
							} else if ("regionName".equals(nameField)) {
								region.setRegionName(jp.getText());
							} else if ("regionType".equals(nameField)) {
								region.setRegionType(jp.getIntValue());
							} 
						}
						list.add(region);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private AddressResponse addressRes(String jsonString) {
		AddressResponse res = new AddressResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					Address addr = new Address();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("addressId".equals(nameField)) {
							addr.setAddressId(jp.getLongValue());
						} else if ("alias".equals(nameField)) {
							addr.setAlias(jp.getText());
						} else if ("consignee".equals(nameField)) {
							addr.setConsignee(jp.getText());
						} else if ("isDefault".equals(nameField)) {
							addr.setIsDefault(jp.getBooleanValue());
						} else if ("mobile".equals(nameField)) {
							addr.setMobile(jp.getText());
						} else if ("postalCode".equals(nameField)) {
							addr.setPostalCode(jp.getText());
						} else if ("regionName".equals(nameField)) {
							addr.setRegionName(jp.getText());
						} else if ("street".equals(nameField)) {
							addr.setStreet(jp.getText());
						} else if ("tel".equals(nameField)) {
							addr.setTel(jp.getText());
						} 
					}
					res.setSuccessData(addr);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private AddressListResponse addressListRes(String jsonString) {
		AddressListResponse res = new AddressListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("pageInfo".equals(fieldname)) {
					PageInfo pi = new PageInfo();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("pageSize".equals(nameField)) {
							pi.setPageSize(jp.getValueAsInt());
						} else if ("nowPage".equals(nameField)) {
							pi.setNowPage(jp.getValueAsInt());
						} else if ("totalResult".equals(nameField)) {
							pi.setTotalResult(jp.getValueAsInt());
						} else if ("orderField".equals(nameField)) {
							pi.setOrderField(jp.getText());
						} else if ("linkCount".equals(nameField)) {
							pi.setLinkCount(jp.getValueAsInt());
						} else if ("orderType".equals(nameField)) {
							pi.setOrderType(jp.getValueAsInt());
						}
					}
					res.setPageInfo(pi);
				} else if ("successData".equals(fieldname)) {
					List<Address> list = new ArrayList<Address>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						Address addr = new Address();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("addressId".equals(nameField)) {
								addr.setAddressId(jp.getLongValue());
							} else if ("alias".equals(nameField)) {
								addr.setAlias(jp.getText());
							} else if ("consignee".equals(nameField)) {
								addr.setConsignee(jp.getText());
							} else if ("isDefault".equals(nameField)) {
								addr.setIsDefault(jp.getBooleanValue());
							} else if ("mobile".equals(nameField)) {
								addr.setMobile(jp.getText());
							} else if ("postalCode".equals(nameField)) {
								addr.setPostalCode(jp.getText());
							} else if ("regionName".equals(nameField)) {
								addr.setRegionName(jp.getText());
							} else if ("street".equals(nameField)) {
								addr.setStreet(jp.getText());
							} else if ("tel".equals(nameField)) {
								addr.setTel(jp.getText());
							} 
						}
						list.add(addr);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private ShoppingAddResponse shoppingAddRes(String jsonString) {
		ShoppingAddResponse res = new ShoppingAddResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					Shopping shopping = new Shopping();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("buyCount".equals(nameField)) {
							shopping.setBuyCount(jp.getFloatValue());
						} else if ("memberId".equals(nameField)) {
							shopping.setMemberId(jp.getLongValue());
						} else if ("shoppingId".equals(nameField)) {
							shopping.setShoppingId(jp.getLongValue());
						} 
					}
					res.setSuccessData(shopping);
				} else if ("failData".equals(fieldname)) {
					Shopping shopping = new Shopping();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("buyCount".equals(nameField)) {
							shopping.setBuyCount(jp.getFloatValue());
						} else if ("memberId".equals(nameField)) {
							shopping.setMemberId(jp.getLongValue());
						} else if ("shoppingId".equals(nameField)) {
							shopping.setShoppingId(jp.getLongValue());
						} 
					}
					res.setFailData(shopping);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private IntegerResponse integerRes(String jsonString) {
		IntegerResponse res = new IntegerResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					res.setSuccessData(jp.getIntValue());
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private ShoppingCartListResponse cartListRes(String jsonString) {
		ShoppingCartListResponse res = new ShoppingCartListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("pageInfo".equals(fieldname)) {
					PageInfo pi = new PageInfo();
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("pageSize".equals(nameField)) {
							pi.setPageSize(jp.getValueAsInt());
						} else if ("nowPage".equals(nameField)) {
							pi.setNowPage(jp.getValueAsInt());
						} else if ("totalResult".equals(nameField)) {
							pi.setTotalResult(jp.getValueAsInt());
						} else if ("orderField".equals(nameField)) {
							pi.setOrderField(jp.getText());
						} else if ("linkCount".equals(nameField)) {
							pi.setLinkCount(jp.getValueAsInt());
						} else if ("orderType".equals(nameField)) {
							pi.setOrderType(jp.getValueAsInt());
						}
					}
					res.setPageInfo(pi);
				} else if ("successData".equals(fieldname)) {
					List<ShoppingCart> list = new ArrayList<ShoppingCart>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						ShoppingCart cart = new ShoppingCart();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("cartId".equals(nameField)) {
								cart.setCartId(jp.getLongValue());
							} else if ("store".equals(nameField)) {
								Store store = new Store();
								while (jp.nextToken() != JsonToken.END_OBJECT) {
									nameField = jp.getCurrentName();
									jp.nextToken();
									if ("isNewOrder".equals(nameField)) {
										store.setIsNewOrder(jp.getBooleanValue());
									} else if ("name".equals(nameField)) {
										store.setName(jp.getText());
									} else if ("storeId".equals(nameField)) {
										store.setStoreId(jp.getLongValue());
									} 
								}
								cart.setStore(store);
							} else if ("shoppings".equals(nameField)) {
								Set<Shopping> shoppings = new HashSet<Shopping>();
								while (jp.nextToken() != JsonToken.END_ARRAY) {
									Shopping shopping = new Shopping();
									while (jp.nextToken() != JsonToken.END_OBJECT) {
										nameField = jp.getCurrentName();
										jp.nextToken();
										if ("buyCount".equals(nameField)) {
											shopping.setBuyCount(jp.getFloatValue());
										} else if ("memberId".equals(nameField)) {
											shopping.setMemberId(jp.getLongValue());
										} else if ("shoppingId".equals(nameField)) {
											shopping.setShoppingId(jp.getLongValue());
										} else if ("storeGoods".equals(nameField)) {
											StoreGoods sg = new StoreGoods();
											while (jp.nextToken() != JsonToken.END_OBJECT) {
												nameField = jp.getCurrentName();
												jp.nextToken();
												if ("storeGoodsId".equals(nameField)) {
													sg.setStoreGoodsId(jp.getValueAsLong());
												} else if ("code".equals(nameField)) {
													sg.setCode(jp.getText());
												} else if ("name".equals(nameField)) {
													sg.setName(jp.getText());
												} else if ("sellStatus".equals(nameField)) {
													sg.setSellStatus(jp.getValueAsInt());
												} else if ("unit".equals(nameField)) {
													sg.setUnit(jp.getText());
												} else if ("price".equals(nameField)) {
													sg.setPrice(jp.getFloatValue());
												} else if ("stock".equals(nameField)) {
													sg.setStock(jp.getFloatValue());
												} else if ("buyDef".equals(nameField)) {
													sg.setBuyDef(jp.getFloatValue());
												} else if ("buyMin".equals(nameField)) {
													sg.setBuyMin(jp.getFloatValue());
												} else if ("buyExt".equals(nameField)) {
													sg.setBuyExt(jp.getFloatValue());
												} else if ("promotePrice".equals(nameField)) {
													sg.setPromotePrice(jp.getFloatValue());
												} else if ("promoteType".equals(nameField)) {
													sg.setPromoteType(jp.getValueAsInt());
												} else if ("pic".equals(nameField)) {
													sg.setPic(jp.getText());
												} else if ("focusOrder".equals(nameField)) {
													sg.setFocusOrder(jp.getValueAsInt());
												} else if ("focusStatus".equals(nameField)) {
													sg.setFocusStatus(jp.getValueAsInt());
												} else if ("sellTime".equals(nameField)) {
													sg.setSellTime(jp.getValueAsLong());
												} else if ("dispatch".equals(nameField)) {
													sg.setDispatch(jp.getText());
												} else if ("dealAll".equals(nameField)) {
													sg.setDealAll(jp.getValueAsInt());
												} else if ("dealMonth".equals(nameField)) {
													sg.setDealMonth(jp.getValueAsInt());
												} else if ("dealTime".equals(nameField)) {
													sg.setDealTime(jp.getValueAsLong());
												} else if ("description".equals(nameField)) {
													sg.setDescription(jp.getText());
												} else if ("updateTime".equals(nameField)) {
													sg.setUpdateTime(jp.getValueAsLong());
												} else if ("addTime".equals(nameField)) {
													sg.setAddTime(jp.getValueAsLong());
												} else if ("keywords".equals(nameField)) {
													sg.setKeywords(jp.getText());
												} else if ("buyUnit".equals(nameField)) {
													sg.setBuyUnit(jp.getText());
												} else if ("buyUnitMin".equals(nameField)) {
													sg.setBuyUnitMin(jp.getValueAsDouble());
												} else if ("buyUnitMax".equals(nameField)) {
													sg.setBuyUnitMax(jp.getValueAsDouble());
												} else if ("sort".equals(nameField)) {
													sg.setSort(jp.getValueAsLong());
												} else if ("dealWeek".equals(nameField)) {
													sg.setDealWeek(jp.getValueAsInt());
												} 
											}
											shopping.setStoreGoods(sg);
										} 
									}
									shoppings.add(shopping);
								}
								cart.setShoppings(shoppings);
							} 
						}
						list.add(cart);
					}
					res.setSuccessData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private OrderAddFormResponse orderAddFormRes(String jsonString) {
		OrderAddFormResponse res = new OrderAddFormResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					OrderAddFormData formData = new OrderAddFormData();
					res.setSuccessData(formData);
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("address".equals(nameField)) {
							Address addr = new Address();
							formData.setAddress(addr);
							while (jp.nextToken() != JsonToken.END_OBJECT) {
								nameField = jp.getCurrentName();
								jp.nextToken();
								if ("addressId".equals(nameField)) {
									addr.setAddressId(jp.getLongValue());
								} else if ("alias".equals(nameField)) {
									addr.setAlias(jp.getText());
								} else if ("consignee".equals(nameField)) {
									addr.setConsignee(jp.getText());
								} else if ("isDefault".equals(nameField)) {
									addr.setIsDefault(jp.getBooleanValue());
								} else if ("mobile".equals(nameField)) {
									addr.setMobile(jp.getText());
								} else if ("postalCode".equals(nameField)) {
									addr.setPostalCode(jp.getText());
								} else if ("regionName".equals(nameField)) {
									addr.setRegionName(jp.getText());
								} else if ("street".equals(nameField)) {
									addr.setStreet(jp.getText());
								} else if ("tel".equals(nameField)) {
									addr.setTel(jp.getText());
								}
							}
							
						} else if ("shoppingCarts".equals(nameField)) {
							List<ShoppingCart> carts = new ArrayList<ShoppingCart>();
							formData.setShoppingCarts(carts);
							while (jp.nextToken() != JsonToken.END_ARRAY) {
								ShoppingCart cart = new ShoppingCart();
								carts.add(cart);
								while (jp.nextToken() != JsonToken.END_OBJECT) {
									nameField = jp.getCurrentName();
									jp.nextToken();
									if ("cartId".equals(nameField)) {
										cart.setCartId(jp.getLongValue());
									} else if ("store".equals(nameField)) {
										Store store = new Store();
										cart.setStore(store);
										while (jp.nextToken() != JsonToken.END_OBJECT) {
											nameField = jp.getCurrentName();
											jp.nextToken();
											if ("isNewOrder".equals(nameField)) {
												store.setIsNewOrder(jp.getBooleanValue());
											} else if ("name".equals(nameField)) {
												store.setName(jp.getText());
											} else if ("storeId".equals(nameField)) {
												store.setStoreId(jp.getLongValue());
											} else if ("expresses".equals(nameField)) {
												Set<Express> expresses = new HashSet<Express>();
												while (jp.nextToken() != JsonToken.END_ARRAY) {
													Express express = new Express();
													while (jp.nextToken() != JsonToken.END_OBJECT) {
														nameField = jp.getCurrentName();
														jp.nextToken();
														if ("expressId".equals(nameField)) {
															express.setExpressId(jp.getLongValue());
														} else if ("expressName".equals(nameField)) {
															express.setExpressName(jp.getText());
														} 
													}
													expresses.add(express);
												}
												store.setExpresses(expresses);
											} else if ("payTypes".equals(nameField)) {
												Set<PayType> payTypes = new HashSet<PayType>();
												store.setPayTypes(payTypes);
												while (jp.nextToken() != JsonToken.END_ARRAY) {
													PayType payType = new PayType();
													payTypes.add(payType);
													while (jp.nextToken() != JsonToken.END_OBJECT) {
														nameField = jp.getCurrentName();
														jp.nextToken();
														if ("typeId".equals(nameField)) {
															payType.setTypeId(jp.getLongValue());
														} else if ("typeName".equals(nameField)) {
															payType.setTypeName(jp.getText());
														} 
													}
												}
											}
										}
									} else if ("shoppings".equals(nameField)) {
										Set<Shopping> shoppings = new HashSet<Shopping>();
										cart.setShoppings(shoppings);
										while (jp.nextToken() != JsonToken.END_ARRAY) {
											Shopping shopping = new Shopping();
											shoppings.add(shopping);
											while (jp.nextToken() != JsonToken.END_OBJECT) {
												nameField = jp.getCurrentName();
												jp.nextToken();
												if ("buyCount".equals(nameField)) {
													shopping.setBuyCount(jp.getFloatValue());
												} else if ("memberId".equals(nameField)) {
													shopping.setMemberId(jp.getLongValue());
												} else if ("shoppingId".equals(nameField)) {
													shopping.setShoppingId(jp.getLongValue());
												} else if ("storeGoods".equals(nameField)) {
													StoreGoods sg = new StoreGoods();
													shopping.setStoreGoods(sg);
													while (jp.nextToken() != JsonToken.END_OBJECT) {
														nameField = jp.getCurrentName();
														jp.nextToken();
														if ("storeGoodsId".equals(nameField)) {
															sg.setStoreGoodsId(jp.getValueAsLong());
														} else if ("code".equals(nameField)) {
															sg.setCode(jp.getText());
														} else if ("name".equals(nameField)) {
															sg.setName(jp.getText());
														} else if ("sellStatus".equals(nameField)) {
															sg.setSellStatus(jp.getValueAsInt());
														} else if ("unit".equals(nameField)) {
															sg.setUnit(jp.getText());
														} else if ("price".equals(nameField)) {
															sg.setPrice(jp.getFloatValue());
														} else if ("stock".equals(nameField)) {
															sg.setStock(jp.getFloatValue());
														} else if ("buyDef".equals(nameField)) {
															sg.setBuyDef(jp.getFloatValue());
														} else if ("buyMin".equals(nameField)) {
															sg.setBuyMin(jp.getFloatValue());
														} else if ("buyExt".equals(nameField)) {
															sg.setBuyExt(jp.getFloatValue());
														} else if ("promotePrice".equals(nameField)) {
															sg.setPromotePrice(jp.getFloatValue());
														} else if ("promoteType".equals(nameField)) {
															sg.setPromoteType(jp.getValueAsInt());
														} else if ("pic".equals(nameField)) {
															sg.setPic(jp.getText());
														} else if ("focusOrder".equals(nameField)) {
															sg.setFocusOrder(jp.getValueAsInt());
														} else if ("focusStatus".equals(nameField)) {
															sg.setFocusStatus(jp.getValueAsInt());
														} else if ("sellTime".equals(nameField)) {
															sg.setSellTime(jp.getValueAsLong());
														} else if ("dispatch".equals(nameField)) {
															sg.setDispatch(jp.getText());
														} else if ("dealAll".equals(nameField)) {
															sg.setDealAll(jp.getValueAsInt());
														} else if ("dealMonth".equals(nameField)) {
															sg.setDealMonth(jp.getValueAsInt());
														} else if ("dealTime".equals(nameField)) {
															sg.setDealTime(jp.getValueAsLong());
														} else if ("description".equals(nameField)) {
															sg.setDescription(jp.getText());
														} else if ("updateTime".equals(nameField)) {
															sg.setUpdateTime(jp.getValueAsLong());
														} else if ("addTime".equals(nameField)) {
															sg.setAddTime(jp.getValueAsLong());
														} else if ("keywords".equals(nameField)) {
															sg.setKeywords(jp.getText());
														} else if ("buyUnit".equals(nameField)) {
															sg.setBuyUnit(jp.getText());
														} else if ("buyUnitMin".equals(nameField)) {
															sg.setBuyUnitMin(jp.getValueAsDouble());
														} else if ("buyUnitMax".equals(nameField)) {
															sg.setBuyUnitMax(jp.getValueAsDouble());
														} else if ("sort".equals(nameField)) {
															sg.setSort(jp.getValueAsLong());
														} else if ("dealWeek".equals(nameField)) {
															sg.setDealWeek(jp.getValueAsInt());
														} 
													}
												} 
											}
										}										
									} 
								}
							}
						}
					}
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private OrderAddResponse orderAddRes(String jsonString) {
		OrderAddResponse res = new OrderAddResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("failData".equals(fieldname)) {
					List<StoreGoods> list = new ArrayList<StoreGoods>();
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						StoreGoods sg = new StoreGoods();
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("storeGoodsId".equals(nameField)) {
								sg.setStoreGoodsId(jp.getValueAsLong());
							} else if ("code".equals(nameField)) {
								sg.setCode(jp.getText());
							} else if ("name".equals(nameField)) {
								sg.setName(jp.getText());
							} else if ("sellStatus".equals(nameField)) {
								sg.setSellStatus(jp.getValueAsInt());
							} else if ("unit".equals(nameField)) {
								sg.setUnit(jp.getText());
							}else if ("price".equals(nameField)) {
								sg.setPrice(jp.getFloatValue());
							} else if ("stock".equals(nameField)) {
								sg.setStock(jp.getFloatValue());
							} else if ("buyDef".equals(nameField)) {
								sg.setBuyDef(jp.getFloatValue());
							} else if ("buyMin".equals(nameField)) {
								sg.setBuyMin(jp.getFloatValue());
							} else if ("buyExt".equals(nameField)) {
								sg.setBuyExt(jp.getFloatValue());
							} else if ("promotePrice".equals(nameField)) {
								sg.setPromotePrice(jp.getFloatValue());
							} else if ("promoteType".equals(nameField)) {
								sg.setPromoteType(jp.getValueAsInt());
							} else if ("pic".equals(nameField)) {
								sg.setPic(jp.getText());
							} else if ("focusOrder".equals(nameField)) {
								sg.setFocusOrder(jp.getValueAsInt());
							} else if ("focusStatus".equals(nameField)) {
								sg.setFocusStatus(jp.getValueAsInt());
							} else if ("sellTime".equals(nameField)) {
								sg.setSellTime(jp.getValueAsLong());
							} else if ("dispatch".equals(nameField)) {
								sg.setDispatch(jp.getText());
							} else if ("dealAll".equals(nameField)) {
								sg.setDealAll(jp.getValueAsInt());
							} else if ("dealMonth".equals(nameField)) {
								sg.setDealMonth(jp.getValueAsInt());
							} else if ("dealTime".equals(nameField)) {
								sg.setDealTime(jp.getValueAsLong());
							} else if ("description".equals(nameField)) {
								sg.setDescription(jp.getText());
							} else if ("updateTime".equals(nameField)) {
								sg.setUpdateTime(jp.getValueAsLong());
							} else if ("addTime".equals(nameField)) {
								sg.setAddTime(jp.getValueAsLong());
							} else if ("keywords".equals(nameField)) {
								sg.setKeywords(jp.getText());
							} else if ("buyUnit".equals(nameField)) {
								sg.setBuyUnit(jp.getText());
							} else if ("buyUnitMin".equals(nameField)) {
								sg.setBuyUnitMin(jp.getValueAsDouble());
							} else if ("buyUnitMax".equals(nameField)) {
								sg.setBuyUnitMax(jp.getValueAsDouble());
							} else if ("sort".equals(nameField)) {
								sg.setSort(jp.getValueAsLong());
							} else if ("dealWeek".equals(nameField)) {
								sg.setDealWeek(jp.getValueAsInt());
							}
						}
						list.add(sg);
					}
					res.setFailData(list);
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private MemberNoticeListResponse memberNoticeListRes(String jsonString) {
		MemberNoticeListResponse res = new MemberNoticeListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("pageInfo".equals(fieldname)) {
					PageInfo pi = new PageInfo();
					res.setPageInfo(pi);
					while (jp.nextToken() != JsonToken.END_OBJECT) {
						String nameField = jp.getCurrentName();
						jp.nextToken();
						if ("pageSize".equals(nameField)) {
							pi.setPageSize(jp.getValueAsInt());
						} else if ("nowPage".equals(nameField)) {
							pi.setNowPage(jp.getValueAsInt());
						} else if ("totalResult".equals(nameField)) {
							pi.setTotalResult(jp.getValueAsInt());
						} else if ("orderField".equals(nameField)) {
							pi.setOrderField(jp.getText());
						} else if ("linkCount".equals(nameField)) {
							pi.setLinkCount(jp.getValueAsInt());
						} else if ("orderType".equals(nameField)) {
							pi.setOrderType(jp.getValueAsInt());
						}
					}
				} else if ("successData".equals(fieldname)) {
					List<MemberNotice> list = new ArrayList<MemberNotice>();
					res.setSuccessData(list);
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						MemberNotice mn = new MemberNotice();
						list.add(mn);
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("status".equals(nameField)) {
								mn.setStatus(jp.getIntValue());
							} else if ("notice".equals(nameField)) {
								Notice notice = new Notice();
								mn.setNotice(notice);
								while (jp.nextToken() != JsonToken.END_OBJECT) {
									nameField = jp.getCurrentName();
									jp.nextToken();
									if ("addTime".equals(nameField)) {
										notice.setAddTime(jp.getLongValue());
									} else if ("content".equals(nameField)) {
										notice.setContent(jp.getText());
									} else if ("noticeId".equals(nameField)) {
										notice.setNoticeId(jp.getLongValue());
									} else if ("summary".equals(nameField)) {
										notice.setSummary(jp.getText());
									} else if ("title".equals(nameField)) {
										notice.setTitle(jp.getText());
									} else if ("type".equals(nameField)) {
										notice.setType(jp.getIntValue());
									} 
								}
							} 
						}
					}
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private SecurityQuestionListResponse securityQuestionListRes(String jsonString) {
		SecurityQuestionListResponse res = new SecurityQuestionListResponse();
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			jp.nextToken();
			while (jp.nextToken() != JsonToken.END_OBJECT) {
				String fieldname = jp.getCurrentName();
				jp.nextToken();
				if ("status".equals(fieldname)) {
					res.setStatus(jp.getText());
				} else if ("code".equals(fieldname)) {
					res.setCode(jp.getText());
				} else if ("message".equals(fieldname)) {
					res.setMessage(jp.getText());
				} else if ("cachePeriod".equals(fieldname)) {
					res.setCachePeriod(jp.getValueAsLong());
				} else if ("successData".equals(fieldname)) {
					List<SecurityQuestion> list = new ArrayList<SecurityQuestion>();
					res.setSuccessData(list);
					while (jp.nextToken() != JsonToken.END_ARRAY) {
						SecurityQuestion sq = new SecurityQuestion();
						list.add(sq);
						while (jp.nextToken() != JsonToken.END_OBJECT) {
							String nameField = jp.getCurrentName();
							jp.nextToken();
							if ("answer".equals(nameField)) {
								sq.setAnswer(jp.getText());
							} else if ("question".equals(nameField)) {
								sq.setQuestion(jp.getText());
							} else if ("questionId".equals(nameField)) {
								sq.setQuestionId(jp.getLongValue());
							} 
						}
					}
				} 
			}
			jp.close();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	private String getResponseBodyAsString(HttpResponse response)
			throws IOException {
		String result = null;
		GZIPInputStream gzin;
		if (response.getEntity().getContentEncoding() != null
				&& response.getEntity().getContentEncoding().getValue()
						.toLowerCase().indexOf("gzip") > -1) {
			InputStream is = response.getEntity().getContent();
			gzin = new GZIPInputStream(is);

			InputStreamReader isr = new InputStreamReader(gzin, "iso-8859-1");
			java.io.BufferedReader br = new java.io.BufferedReader(isr);
			StringBuilder sb = new StringBuilder();
			String tempbf;
			while ((tempbf = br.readLine()) != null) {
				sb.append(tempbf);
				sb.append("\r\n");
			}
			isr.close();
			gzin.close();
			result = sb.toString();
			result = new String(result.getBytes("iso-8859-1"),
					config.getEncoding());
		} else {
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, config.getEncoding());
			if (entity != null) {
				//EntityUtils.consume(entity);
				if (entity.isStreaming()) {
					final InputStream instream = entity.getContent();
					if (instream != null) {
						instream.close();
					}
				}
			}
		}
		return result;
	}
}
